<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/12 0012
 * Time: 9:50
 */

namespace app\data\controller;


use think\Db;
use think\Log;

class Beili extends Base
{
    private $currency =  ['btc','eth','bch','ltc','eos','xrp'];
    private $now;


    public function __construct()
    {
        parent::__construct();
        $this->now = date('YmdH',strtotime('-1hour'));

    }

  	public function GetBeiliData(){
        try{
            foreach ($this->currency as $k){
                $url = 'https://api.huobi.pro/market/history/kline?symbol='.$k.'usdt&period=60min&size=1';
                $ref = json_decode($this->cUrl($url))->data[0];
                $map = [
                    'title'	=>$k,
                    'time'	=>date('YmdH',$ref->id-3600),
                ];
                $result = Db::name('beili')->where($map)->find();
                if(!$result){
                    $map['price'] = $ref->open;
                    Db::name('beili')->insert($map);
                }
            }
            return 1;
        }catch (\Exception $e) {
            return $this->GetBeiliData();
        }
    }
  
  
    //todo 每小时计数
    public function BeiliOneTimes(){
        $data = Db::name('beili_set')->where(['id'=>['in',[1,2,7,8]]])->select();
        $this->Business($data,1);

    }
    //todo 每4小时计数
    public function BeiliFourTimes(){
         $data = Db::name('beili_set')->where(['id'=>['in',[3,4,9,10]]])->select();
         $this->Business($data,4);
    }
    //todo 每天计数
    public function BeiliDayTimes(){      
        $data = Db::name('beili_set')->where(['id'=>['in',[5,6,11,12]]])->select();
        $this->Business($data,24);
    }
    //todo 背离业务处理
    //todo 背离业务处理
    private function Business($data,$type)
    {
        
        
        $size = '6';
        $date = '60min';
        if($type  == 4)$date = '4hour';
        if($type  == 24)$date = '1day';
        Db::startTrans();
        try{
            foreach ($this->currency as $k) {
                $url = 'https://api.huobi.pro/market/history/kline?symbol='.$k.'usdt&period='.$date.'&size='.$size;
                $ref = json_decode($this->cUrl($url));
                $close = $ref->data[1]->close;
                $timeclose = $ref->data[5]->close;
                if($close > $timeclose){
                    $status = 1;
                    $ress = 2;
                    $x = $data[0];
                    $y = $data[1];
                }elseif($close < $timeclose){
                    $status = 2;
                    $ress = 1;
                    $x = $data[2];
                    $y = $data[3];
                }else{
                    return false;
                }
                
                
                $map = ['title' => $k, 'type' => $type, 'status' => $status];
                Db::name('beili_data')->where($map)->setInc('num', 1);
                Db::name('beili_data')->where($map)->update(['time'=>time()]);
                $num = Db::name('beili_data')->where($map)->value('num');
                //todo 推送消息
                if ($num >= $x['content'] && $num < $y['content']) {
                    $msg = $this->GetMessage($x['msg'], $x['title'], ['title' => $k, 'price' => $ref['price']]);
                    $this->MsgDb($k, $type, $msg);
                }
                if ($num >= $y['content']) {
                    $msg = $this->GetMessage($y['msg'], $y['title'], ['title' => $k, 'price' => $ref['price']]);
                    $this->MsgDb($k, $type, $msg);
                }
                $map = ['title' => $k, 'type' => $type,'status'=>$ress];
                Db::name('beili_data')->where($map)->update(['num' => 0, 'time' => time()]);

            }
            Db::commit();
            return 1;
        }catch (\Exception $e){
            Db::rollback();
            $this->Business($data,$type);
        }
    }
    //todo 发送消息
    private function MsgDb($title,$time,$msg){
        $umap = ['title'=>strtoupper($title),'time'=>$time];
        $user = Db::name('beili_user')->where($umap)->column('uid');     	
        if(!$user)return false;
        $this->SendMsg($msg,$user,7);
        $reg_id = $this->GetPushReg($user);
        if($reg_id)$this->PushMsg($msg['content'],$msg['title'],$reg_id);
        return;
    }
    //todo 背离提醒时间转换
    private function DateChange($time){
        if($time != 24){
            $t4 = date('YmdH',strtotime('-1hour')-3600*$time*4);
        }else{
            $t4 = date('Ymd',strtotime('-1day')-3600*$time*4).'00';
        }
        return $t4;
    }
  
  	public function updateUser(){
    	$time = time()-86400*2;
      	$map = ['update_time'=>['<',$time]];
      	$res = Db::name('user')->where($map)->update(['reg_id'=>null]);
    }

}